/*
* This file is part of WC2.
*
* WC2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* WC2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with WC2; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*	Main.cpp
*/

#include "allegro.h"
#include "map.h"
#include "player.h"
#include "functions.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>

#define BLACK makecol(0,0,0)
#define WHITE makecol(255,255,255)

const int scrx = 480;
const int scry = 600;

DATAFILE *mapfile = NULL;
BITMAP *buffer = NULL;
BITMAP *buffer2 = NULL;
map *new_map = new map(480,480,16,16);



	//MAPS
	int town[30][30] = 
	{
		30,15,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,25,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,28,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,25,29,29,29,29,29,29,29,29,24,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,11,11,11,11,11,27,7,6,4,6,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,10,11,11,11,11,27,7,4,32,3,32,26,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,13,10,10,10,10,10,10,10,27,17,17,17,31,8,27,11,11,11,11,11,11,11,11,26,11,11,11,26,
		30,20,12,19,19,19,19,19,19,19,26,17,17,17, 3,5,27,11,11,11,11,11,11,11,11,27,11,11,33,27,
		10,10,10,10,10,10,10,10,10,10,27,17,17,17,31,31,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,10,11,11,11,11,27,0,7,6,4,31,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,10,11,11,11,11,11,11,11,27,8,31,7,3,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,7,4,31,8,8,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,8,8,7,32,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,22,29,24,8,8,5,22,29,29,29,29,29,29,29,29,23,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,22,29,24,31,8,9,8,6,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,7,32,32,6,4,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,8,8,9,8,9,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,10,11,11,11,11,11,11,10,22,29,29,29,29,29,29,29,29,29,29,29,29,29,29,28,
		11,11,11,11,11,11,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		19,19,19,16,11,11,10,11,11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,10,10,10,11,11,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,10,10,10,10,11,11,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27
	};

	int ruins1[30][30] = 
	{
		30,15,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,25,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,28,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,25,29,29,29,29,29,29,29,29,24,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,11,11,11,11,11,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,11,11,11,11,11,27,7,6,4,6,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,10,11,11,11,11,27,7,4,32,3,32,26,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,13,10,10,10,10,10,10,10,27,17,17,17,31,8,27,11,11,11,11,11,11,11,11,26,11,11,11,27,
		30,20,12,19,19,19,19,19,19,19,26,17,17,17, 3,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,10,10,10,10,10,27,17,17,17,31,31,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		10,10,10,10,10,10,11,11,11,11,27,0,7,6,4,31,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,10,11,11,11,11,11,11,11,27,8,31,7,3,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,7,4,31,8,8,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,27,8,8,7,32,5,27,11,11,11,11,11,11,11,11,27,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,22,29,24,8,8,5,22,29,29,29,29,29,29,29,29,23,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,22,29,24,31,8,9,8,6,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,7,32,32,6,4,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,8,8,9,8,9,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,11,10,11,11,11,11,11,11,10,22,29,29,29,29,29,29,29,29,29,29,29,29,29,29,28,
		11,11,11,11,11,11,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		19,19,19,16,11,11,10,11,11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,10,10,10,11,11,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27,
		11,11,11,11,11,10,10,10,10,11,11,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,27
	};

int random;

//Timer
volatile long timer = 0;  

void increment_timer()
{
  timer++; 
}
END_OF_FUNCTION(increment_timer);

//Draw Map
void Draw_Map(BITMAP *sbuff, BITMAP *dbuff, int map[30][30])
{


	int i;
	int j;

	


	for (i = 0; i < 30; ++i)
	{
		
		for (j = 0; j < 30; ++j)
		{
			new_map->mapt[i].tilex = i * 16;
			new_map->mapt[j].tiley = j * 16;
			
			switch(map[i][j])
			{
				case 0:		//bar
					blit((BITMAP*)mapfile[0].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].building = 1;
					break;
				case 1:		//bazaar
					blit((BITMAP*)mapfile[1].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 2:		//City Road "T"
					blit((BITMAP*)mapfile[2].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 3:		//City Road 4 way
					blit((BITMAP*)mapfile[3].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 4:		//City Road "T" Left
					blit((BITMAP*)mapfile[4].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 5:		//City Road "T" North
					blit((BITMAP*)mapfile[5].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 6:		//City Road "T" Right
					blit((BITMAP*)mapfile[6].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 7:		//City Road "T" South
					blit((BITMAP*)mapfile[7].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 8:		//City Cobblestone 1
					blit((BITMAP*)mapfile[8].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 9:		//City Cobblestone 2
					blit((BITMAP*)mapfile[9].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 10:		//Forest
					blit((BITMAP*)mapfile[10].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 1;
					break;
				case 11:		//Grass
					blit((BITMAP*)mapfile[11].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 2;
					break;
				case 12:		//Grass Road "T"
					blit((BITMAP*)mapfile[12].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 13:		//Inn 1
					blit((BITMAP*)mapfile[13].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].building = 1;
					break;
				case 14:		//Inn 2
					blit((BITMAP*)mapfile[14].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 15:		//Lab
					blit((BITMAP*)mapfile[15].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].building = 3;
					new_map->tblock[i][j].terrain = 3;
					break;
				case 16:		//Mansion
					blit((BITMAP*)mapfile[16].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].building = 4;
					new_map->tblock[i][j].terrain = 4;
					break;
				case 17:		//Market
					blit((BITMAP*)mapfile[17].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 19:		//Road Grass
					blit((BITMAP*)mapfile[19].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 20:		//Road Water
					blit((BITMAP*)mapfile[20].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 21:		//Temple
					blit((BITMAP*)mapfile[21].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 22:		//Wall Corner 1
					blit((BITMAP*)mapfile[22].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 23:		//Wall Corner 2
					blit((BITMAP*)mapfile[23].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 24:		//Wall Corner 3
					blit((BITMAP*)mapfile[24].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 25:		//Wall Corner 4
					blit((BITMAP*)mapfile[25].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 26:		//Wall Gate
					blit((BITMAP*)mapfile[26].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					break;
				case 27:		//Wall Horizontal
					blit((BITMAP*)mapfile[27].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 28:		//Wall "T"
					blit((BITMAP*)mapfile[28].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 29:		//Wall Vertical 
					blit((BITMAP*)mapfile[29].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 1;
					break;
				case 30:		//Windy Road
					blit((BITMAP*)mapfile[30].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 31:		//City Road Horizontal
					blit((BITMAP*)mapfile[31].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 32:		//City Road Horizontal
					blit((BITMAP*)mapfile[32].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].terrain = 8;
					break;
				case 33:		//Crossed Swords
					blit((BITMAP*)mapfile[33].dat,sbuff,0,0,i * 16,j * 16, 480, 480);
					new_map->tblock[i][j].blocked = 0;
					new_map->tblock[i][j].building = 2;
					break;
			}

		}
		//blit(sbuff,dbuff,0,0,0,0,scrx,scry);
	}
	

}



int main(int argc, char* argv[]) 
{

	player *plyr = new player();
	open_binary_file();
	log_to_binary_file(plyr->str);

//	save_character(plyr);

	log_to_file(plyr->str);

	if (allegro_init()) 
	{
		allegro_message("Cannot initalize Allegro.\n");
		return 1;
	}

	set_window_title("WC2");

	if (install_keyboard()) 
	{
		allegro_message("Cannot initalize keyboard input.\n");
		return 1;
	}

	set_color_depth(24);
	if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) 
	{
        allegro_message("Video Error: %s.\n", allegro_error);
		return 1;
    }

	install_timer(); 

	LOCK_VARIABLE(speed_counter);
	LOCK_FUNCTION(increment_speed_counter);

	install_int_ex(increment_timer, BPS_TO_TIMER(10));

	int px = 128;
	int py = 128;
	
	

	BITMAP *buffer = create_bitmap(scrx,scry);
	BITMAP *buffer2 = create_bitmap(scrx,scry);
	mapfile = load_datafile("wc2.dat");

	while(!key[KEY_ESC])
	{
		while(timer > 0)
		{
			

			if(key[KEY_3] && new_map->tblock[px / 16 + 1][py / 16].get_passx(new_map->mapt[px].get_tilex()) != 1) // && px / 16 + 1 != new_map->get_map_width()) 
			{ 
			   px +=16; 
			} 

			else if(key[KEY_7] && new_map->tblock[px / 16 - 1][py / 16].get_passx(new_map->mapt[px].get_tilex()) != 1)  //&& px / 16 != new_map->get_map_width() - 464) 
			{ 
			   px -=16; 
			}

			else if(key[KEY_1] && new_map->tblock[px / 16][py / 16 - 1].get_passy(new_map->mapt[py].get_tiley()) != 1)  
			{
			   py -=16; 
			}

			else if(key[KEY_5] && new_map->tblock[px / 16][py / 16 + 1].get_passy(new_map->mapt[py].get_tiley()) != 1) 
			{ 
			   py +=16; 
			}

			else if(key[KEY_4] && new_map->tblock[px / 16 + 1][py / 16 + 1].get_pass(new_map->mapt[px / 16].get_tilex(),new_map->mapt[py / 16].get_tiley()) != 1)
			{
				px +=16;
				py +=16;
			}
			else if(key[KEY_2] && new_map->tblock[px / 16 + 1][py / 16 - 1].get_pass(new_map->mapt[px / 16].get_tilex(),new_map->mapt[py / 16].get_tiley()) != 1)
			{
				px +=16;
				py -=16;
			}
			else if(key[KEY_6] && new_map->tblock[px / 16 - 1][py / 16 + 1].get_pass(new_map->mapt[px / 16].get_tilex(),new_map->mapt[py / 16].get_tiley()) != 1)
			{
				px -=16;
				py +=16;
			}
			else if(key[KEY_8] && new_map->tblock[px / 16 - 1][py / 16 - 1].get_pass(new_map->mapt[px / 16].get_tilex(),new_map->mapt[py / 16].get_tiley()) != 1)
			{
				px -=16;
				py -=16;
			}

			else if(key[KEY_S])
			{
				
				switch(new_map->tblock[px / 16][py / 16].building)
				{

				case 1:		//Tavern
					{
						while(!key[KEY_N])
						{
						clear_to_color(buffer,BLACK);
						textprintf_ex(buffer2, font, 20, 480, WHITE, -1, "YOU ARE AT A TAVERN");
						textprintf_ex(buffer2, font, 20, 490, WHITE, -1, "DO YOU WISH TO ENTER(Y/N)");
						blit(buffer2,buffer,0,0,0,0,scrx,scry);
						blit((BITMAP*)mapfile[18].dat,buffer,0,0,px,py,scrx,scry);
						blit(buffer,screen,0,0,0,0,scrx,scry);
							if(key[KEY_Y])
							{
								
								clear_to_color(screen,BLACK);
								clear_to_color(buffer,BLACK);
								clear_to_color(buffer2,BLACK);
								textprintf_ex(buffer2, font, 50, 40, WHITE, -1, "A TAVERN FULL OF ADVENTURERS");
								textprintf_ex(buffer2, font, 50, 50, WHITE, -1, "THE CHARACTERS EAT DRINK AND HAVE A GOOD");
								textprintf_ex(buffer2, font, 50, 60, WHITE, -1, "TIME.");

								random = rand() % 5 + 1;

								if(random == 1)
										{
											while(!key[KEY_SPACE])
											{
											textprintf_ex(buffer2, font, 50, 70, WHITE, -1, "MANY CONVERSATIONS GO ON AROUND YOU,");
											textprintf_ex(buffer2, font, 50, 80, WHITE, -1, "ONE OF WHICH YOU OVERHEAR");
											textprintf_ex(buffer2, font, 50, 100, WHITE, -1, "... WITHOUT A +5 WEAPON WE WON'T LAST");
											textprintf_ex(buffer2, font, 50, 110, WHITE, -1, "FIVE MINUTES PAST THE FIRST TEMPLE ...");
											blit(buffer2,buffer,0,0,0,0,scrx,scry);
											blit(buffer,screen,0,0,0,0,scrx,scry);
											}
										}

								if(random == 2)
										{
											while(!key[KEY_SPACE])
											{
											textprintf_ex(buffer2, font, 50, 70, WHITE, -1, "MANY CONVERSATIONS GO ON AROUND YOU,");
											textprintf_ex(buffer2, font, 50, 80, WHITE, -1, "ONE OF WHICH YOU OVERHEAR");
											textprintf_ex(buffer2, font, 50, 100, WHITE, -1, "... THE PASSWORD IS 'BREAKFAST' ...");
											textprintf_ex(buffer2, font, 50, 110, WHITE, -1, "");
											blit(buffer2,buffer,0,0,0,0,scrx,scry);
											blit(buffer,screen,0,0,0,0,scrx,scry);
											}
										}

								if(random == 3)
										{
											while(!key[KEY_SPACE])
											{
											textprintf_ex(buffer2, font, 50, 70, WHITE, -1, "MANY CONVERSATIONS GO ON AROUND YOU,");
											textprintf_ex(buffer2, font, 50, 80, WHITE, -1, "ONE OF WHICH YOU OVERHEAR");
											textprintf_ex(buffer2, font, 50, 100, WHITE, -1, "... WE WILL NEED AN EMERALD KEY TO ...");
											textprintf_ex(buffer2, font, 50, 110, WHITE, -1, "");
											blit(buffer2,buffer,0,0,0,0,scrx,scry);
											blit(buffer,screen,0,0,0,0,scrx,scry);
											}
										}

								if(random == 4)
										{
											while(!key[KEY_SPACE])
											{
											textprintf_ex(buffer2, font, 50, 70, WHITE, -1, "MANY CONVERSATIONS GO ON AROUND YOU,");
											textprintf_ex(buffer2, font, 50, 80, WHITE, -1, "ONE OF WHICH YOU OVERHEAR");
											textprintf_ex(buffer2, font, 50, 100, WHITE, -1, "...THERE IS A MAGIC SHOP IN THE NORTH-");
											textprintf_ex(buffer2, font, 50, 110, WHITE, -1, "EAST CORNER OF THE INNER WALL ...");
											blit(buffer2,buffer,0,0,0,0,scrx,scry);
											blit(buffer,screen,0,0,0,0,scrx,scry);
											}
										}

								if(random == 5)
										{
											while(!key[KEY_SPACE])
											{
											textprintf_ex(buffer2, font, 50, 100, WHITE, -1, "");
											textprintf_ex(buffer2, font, 50, 110, WHITE, -1, "");
											blit(buffer2,buffer,0,0,0,0,scrx,scry);
											blit(buffer,screen,0,0,0,0,scrx,scry);
											}
											
										}
								break;
										
							}
							

							if(key[KEY_N])
							{
								break;
							}
						
						}
						break;
						
					}

				case 2:
					{
						clear_to_color(screen,BLACK);
						clear_to_color(buffer,BLACK);
						clear_to_color(buffer2,BLACK);

							while(!key[KEY_X])
							{
								textprintf_ex(buffer2, font, 10, 580, WHITE, -1, "(G)reet new adventurers");
								textprintf_ex(buffer2, font, 10, 590, WHITE, -1, "(T)ell the minstrel of your deeds");
								blit(buffer2,buffer,0,0,0,0,scrx,scry);
								blit(buffer,screen,0,0,0,0,scrx,scry);
							}
							break;
					}
				case 3:		//Alchemy Lab
					{
						while(!key[KEY_N])
						{
						clear_to_color(buffer,BLACK);
						textprintf_ex(buffer2, font, 20, 480, WHITE, -1, "YOU ARE AT A LABORATORY");
						textprintf_ex(buffer2, font, 20, 490, WHITE, -1, "DO YOU WISH TO ENTER(Y/N)");
						blit(buffer2,buffer,0,0,0,0,scrx,scry);
						blit((BITMAP*)mapfile[18].dat,buffer,0,0,px,py,scrx,scry);
						blit(buffer,screen,0,0,0,0,scrx,scry);
							if(key[KEY_Y])
							{
								clear_to_color(screen,BLACK);
								clear_to_color(buffer,BLACK);
								clear_to_color(buffer2,BLACK);

								while(!key[KEY_X])
								{

									textprintf_ex(buffer2, font, 10, 40, WHITE, -1, "CURRENT PARTY MEMBERS");
									textprintf_ex(buffer2, font, 10, 50, WHITE, -1, "ID    NAME                    STATUS");
									textprintf_ex(buffer2, font, 10, 240, WHITE, -1, "THE PROPRIETOR ASKS FOR THE ALCHEMIST IN");
									textprintf_ex(buffer2, font, 10, 250, WHITE, -1, "YOUR PARTY (ID OR E(X)IT)");
									blit(buffer2,buffer,0,0,0,0,scrx,scry);
									blit(buffer,screen,0,0,0,0,scrx,scry);
								}
							break;
							}
							

							if(key[KEY_N])
							{
								break;
							}
						}
						break;
					}
					
				case 4:		//Mansion
					{
						clear_to_color(screen,BLACK);
						clear_to_color(buffer,BLACK);
						clear_to_color(buffer2,BLACK);

							while(!key[KEY_SPACE])
							{

							textprintf_ex(buffer2, font, 10, 480, WHITE, -1, "A MANSION");
							//clear(buffer2);
							blit(buffer2,buffer,0,0,0,0,scrx,scry);
							blit(buffer,screen,0,0,0,0,scrx,scry);
							}
					break;
					}

				default:
					{
						clear_to_color(buffer,BLACK);
						textprintf_ex(buffer2, font, 10, 480, WHITE, -1, "NOTHING FOUND");
						blit(buffer2,buffer,0,0,0,0,scrx,scry);
						blit((BITMAP*)mapfile[18].dat,buffer,0,0,px,py,scrx,scry);
						blit(buffer,screen,0,0,0,0,scrx,scry);
						rest(10000/10);
						break;
					}
				}
			

			  
			}

			timer --;
			
		}
			clear_to_color(buffer,BLACK);
			clear_to_color(buffer2,BLACK);	

			Draw_Map(buffer2, buffer, town);

			blit(buffer2,buffer,0,0,0,0,scrx,scry);
			blit((BITMAP*)mapfile[18].dat,buffer,0,0,px,py,scrx,scry);
			textprintf_ex(buffer, font, 10, 480, WHITE, -1, "(S)EARCH AREA,    (C)HECK A CHARACTER,         812");
			textprintf_ex(buffer, font, 10, 490, WHITE, -1, "(M)AKE CAMP                                    7 3");
			textprintf_ex(buffer, font, 10, 500, WHITE, -1, "                                               654");

			switch(new_map->tblock[px / 16][py / 16].get_terrain(px / 16, py / 16))
			{
			case 1:
				textprintf_ex(buffer, font, 10, 510, WHITE, -1, "WOODS");
				break;
			case 2:
				textprintf_ex(buffer, font, 10, 510, WHITE, -1, "GRASS");
				break;
			case 3:
				textprintf_ex(buffer, font, 10, 510, WHITE, -1, "LABORATORIES");
				break;
			case 4:
				textprintf_ex(buffer, font, 10, 510, WHITE, -1, "A MANSION");
				break;
			case 8:
				textprintf_ex(buffer, font, 10, 510, WHITE, -1, "A ROAD");
				break;
			}
			textprintf_ex(buffer, font, 10, 580, WHITE, -1, "px: %i", new_map->mapt[px / 16].get_tilex());
			textprintf_ex(buffer, font, 10, 590, WHITE, -1, "py: %i", new_map->mapt[py / 16].get_tiley());
			
			//textprintf_ex(buffer, font, 10, 580, WHITE, -1, "blocking: %i", new_map->tblock[px / 16][py / 16].get_pass(new_map->mapt[px / 16].get_tilex(),new_map->mapt[py / 16].get_tiley()));
			//textprintf_ex(buffer, font, 500, 540, BLACK, -1, "Map width: %i", new_map->get_map_width());
			//textprintf_ex(buffer, font, 500, 560, BLACK, -1, "Map height: %i", new_map->get_map_height());

			blit(buffer,screen,0,0,0,0,scrx,scry);
	}


	close_log_file();
	return 0;

}
END_OF_MAIN()

